<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form_step\util\fssr;

use builder\helper\Url;

/**
 * Modal类型
 * @author cleverstone
 * @since ym2.0
 */
class Modal extends FssrType
{
    const TYPE = 'modal';

    /**
     * @var array
     */
    private $_options = [
        'route' => '',
        'params' => [],
        'width' => '920px', // 指定modal的宽;
        'height' => '620px',// 指定modal的高;
        'title' => '',//指定modal标题
        'closeBtn' => 0,//是否显示底部关闭按钮
        'titleCloseBtn' => 1,//是否显示标题栏关闭按钮，默认显示
        'titleMaxminBtn' => false,//是否允许全屏最小化，默认否
        'refererWindow' => 'self',//模态框`iframe`的来源窗口，默认当前窗口。
    ];

    /**
     * @param string $context 模态框`iframe`的来源窗口
     * - self
     * - parent
     * - parent.parent.***
     * @return $this
     */
    public function refererWindow($context)
    {
        $this->_options['refererWindow'] = $context;
        return $this;
    }

    /**
     * 显示标题栏全屏最小化按钮
     * @return $this
     */
    public function titleMaxminBtnDisplay()
    {
        $this->_options['titleMaxminBtn'] = true;
        return $this;
    }

    /**
     * 隐藏标题栏关闭按钮
     * @return $this
     */
    public function titleCloseBtnHidden()
    {
        $this->_options['titleCloseBtn'] = 0;
        return $this;
    }

    /**
     * @param string $title 名称标题
     * ```php
     * // 文本标题
     * $this->title('编辑');
     *
     * // 支持HTML
     * $this->title(H5::text('编辑'));
     *
     * ```
     *
     * @return $this
     */
    public function title($title)
    {
        $this->_options['title'] = (string)$title;
        return $this;
    }

    /**
     * @param string|array $params 请求参数
     * one:
     * ```php
     * // 指定成功返回中的字段
     * $this->params(['id']);
     *
     * ```
     *
     * two:
     * ```php
     * // key-value, 字段指定值
     * $this->params(['id' => 1]);
     *
     * ```
     *
     * three:
     * ```php
     * // key-占位符, 字段+字段占位符
     * $this->params(['action' => ':status']);
     *
     * ```
     * @return $this
     */
    public function params($params)
    {
        $this->_options['params'] = (array)$params;
        return $this;
    }

    /**
     * @param string $route 路由或外链
     * ```php
     * 可以直接指定路由
     * $this->route('user/edit');
     *
     * 也可以指定外部链接
     * $this->route('https://ym2.cleverstone.top');
     *
     * ```
     *
     * @return $this
     */
    public function route($route)
    {
        $this->_options['route'] = Url::to($route, '');
        return $this;
    }

    /**
     * @param string $width 模态框的宽, 当type为modal时有效;
     * @return $this
     */
    public function width($width = '100%')
    {
        if (is_numeric($width)) {
            $width .= 'px';
        }

        $this->_options['width'] = $width;
        return $this;
    }

    /**
     * @param string $height 模态框的高, 当type为modal时有效;
     * @return $this
     */
    public function height($height = '100%')
    {
        if (is_numeric($height)) {
            $height .= 'px';
        }

        $this->_options['height'] = $height;
        return $this;
    }

    /**
     * 显示底部关闭按钮
     * @return $this
     */
    public function closeBtn()
    {
        $this->_options['closeBtn'] = 1;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        $options = $this->_options;
        $options['type'] = self::TYPE;

        return $options;
    }
}